<?php
namespace app\home\controller;
use cmf\controller\UserBaseController;
use think\Db;
class SingleController extends UserBaseController
{
    //店铺每天总营业额统计
    public function index(){
        $uinfo=cmf_get_current_user();
        $where='1=1';
        $where .=' and d.storeid='.$uinfo['store_id'];
        $request = input('request.');
        if(!empty($request['start_time'])){
           $where .= ' and DATE( FROM_UNIXTIME( d.ctime ) ) >= "'.$request['start_time'].'"';
        }else{
           $where .= ' and DATE( FROM_UNIXTIME( d.ctime ) ) >= "'.date('Y-m-d',time()).'"';
        }
        if(!empty($request['end_time'])){
           $where .= ' and DATE( FROM_UNIXTIME( d.ctime ) ) <= "'.$request['end_time'].'"';
        }
        if(!empty($request['g_goodsname'])){
            $where .= ' and d.g_goodsname like "%'.$request['g_goodsname'].'%"';
        }
        $order='';
        $norder=isset($request['norder'])?$request['norder']:'';
        if($norder ==2){
            $order='num desc';
        }elseif($norder ==1){
            $order='num asc';
        }
        $morder=isset($request['morder'])?$request['morder']:'';
        if($morder ==2){
            if(strlen($order)){
                $order.=',money desc';
            }else{
                $order='money desc';
            }
        }elseif($morder ==1){
            if(strlen($order))
            {
                $order.=',money asc';
            }else{
                $order='money asc';
            }
        }
        $where .=' and payStatus=1';
        $list=Db::name('order_detail')->alias('d')->join('store s','s.id=d.storeid')->where($where)->order($order)->field('d.id,s.store_name,d.g_goodsname,d.stemeal,d.ctime,sum(d.count) as num,sum(d.total) as money')->group('d.goodsid')->paginate(8);
        //总汇
        $whereone='1=1';
        if(!empty($request['start_time'])){
           $whereone .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.$request['start_time'].'"';
        }else{
           $whereone .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.date('Y-m-d',time()).'"';
       }
       if(!empty($request['end_time'])){
           $whereone .= ' and DATE( FROM_UNIXTIME( ctime ) ) <= "'.$request['end_time'].'"';
       }       
        $dataa['dingdanshu'] = Db::name("order_detail")->where("payStatus=1")->where("storeid=".$uinfo['store_id'])->where($whereone)->count();
        $this->assign('dataa',$dataa);        
        $list->appends($request);
        $page = $list->render();
        $this->assign("page", $list);
        $this->assign("list", $list);
        return $this->fetch();
    }
}